﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using Domain.Dto;
using Domain.Dto.User;
using Gmmy.DomainExtension;
using Gmmy.Utility;
using Infrastructure.Common.Enums;

namespace Domain.Model
{
    public class User : EntityBase
    {
        #region 领域对象属性
        /// <summary>
        /// 编号
        /// </summary>
        [DisplayName(@"编号")]
        [Key]
        public Guid ID { get; set; }

        /// <summary>
        /// 登录名
        /// </summary>
        [DisplayName(@"登录名")]
        [MaxLength(20)]
        public string LoginName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [DisplayName(@"密码")]
        [MaxLength(32)]
        public string PassWord { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        [DisplayName(@"昵称")]
        [MaxLength(20)]
        public string NickName { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        [DisplayName(@"状态")]
        public EnumUserState State { get; set; }

        /// <summary>
        /// 本次登录ip
        /// </summary>
        [DisplayName(@"本次登录ip")]
        public string LastLoginIp { get; set; }

        /// <summary>
        /// 本次登录时间
        /// </summary>
        [DisplayName(@"本次登录时间")]
        public DateTime? LastLoginTime { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [DisplayName(@"创建时间")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        [DisplayName(@"逻辑删除")]
        public bool IsDeleted { get; set; }
        #endregion
        #region 领域对象行为

        public User(){}
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        public User(UpdateUserInput input)
        {
            if (!input.Roles.Any())
            {
                throw new CustomException("请至少为用户选择一个角色!");
            }
            if (string.IsNullOrEmpty(input.LoginName) || string.IsNullOrEmpty(input.NickName) || string.IsNullOrEmpty(input.NewPassword))
            {
                throw new CustomException("用户名、昵称以及密码必填!");
            }
            if (input.NewPassword != input.ConfirmPassword)
            {
                throw new CustomException("密码和密码确认不符!");
            }
            if (input.NewPassword.Length < 6)
            {
                throw new CustomException("密码必须6位以上!");
            }
            ID = new Guid();
            CreateTime = DateTime.Now;
            IsDeleted = false;
            PassWord = EncryptHelper.GetHMACSHA256(input.NewPassword + ID, ID.ToString());
            LoginName = input.LoginName;
            NickName = input.NickName;
        }
        /// <summary>
        /// 修改附加数据
        /// </summary>
        public void ModifyAdditional(UpdateUserInput input)
        {
            if (!input.Roles.Any())
            {
                throw new CustomException("请至少为用户选择一个角色!");
            }
            if (!string.IsNullOrWhiteSpace(input.NickName))
            {
                NickName = input.NickName;
            }
            if (!string.IsNullOrWhiteSpace(input.NewPassword) && !string.IsNullOrWhiteSpace(input.ConfirmPassword))
            {
                if (input.NewPassword != input.ConfirmPassword)
                {
                    throw new CustomException("密码和密码确认不符!");
                }
                if (input.NewPassword.Length < 6)
                {
                    throw new CustomException("密码必须6位以上!");
                }
                PassWord = EncryptHelper.GetHMACSHA256(input.NewPassword + ID, ID.ToString());
            }
            else if (!(string.IsNullOrWhiteSpace(input.NewPassword) &&
                       string.IsNullOrWhiteSpace(input.ConfirmPassword)))
            {
                throw new CustomException("若要修改密码则密码和密码确认必填!");
            }
        }

        /// <summary>
        /// 更新登录信息
        /// </summary>
        public void UpdateLoginInfo(RequestInfo input)
        {
            LastLoginIp = !string.IsNullOrEmpty(input?.IpAddress) ? input.IpAddress : "";
            LastLoginTime = DateTime.Now;
        }

        /// <summary>
        /// 更新状态信息
        /// </summary>
        public void ChangeState(GetInput input)
        {
            if (ID == input.GetAuthInfo().ID && State == EnumUserState.Normal)
            {
                throw new CustomException("无法禁用当前登录账户!");
            }
            State = State == EnumUserState.Normal ? EnumUserState.Locked : EnumUserState.Normal;
        }
        #endregion
    }
}
